﻿
using System.Windows;
using System.Windows.Input;
using Prism.Events;
using Prism.Regions;
using SoonCode.FocusTest.Events;
using SoonCode.FocusTest.Models;
using SoonCode.FocusTest.Utils;
using MessageBox = HandyControl.Controls.MessageBox;

namespace SoonCode.FocusTest.Views
{
    /// <summary>
    /// TeacherWindow.xaml 的交互逻辑
    /// </summary>
    public partial class TeacherWindow : BaseWindow
    {
        private IEventAggregator aggregator;
        private IRegionManager regionManager;
        private static readonly string regionName="teachRegion";
        private SubscriptionToken eventToken;
        public TeacherWindow(IEventAggregator aggregator)
        {
            this.aggregator = aggregator;
            this.regionManager = (new RegionManager()).CreateRegionManager();
            GlobalModel.Instance.TeacherWidnow = this;
            RegionManager.SetRegionManager(this, this.regionManager);
            regionManager.RegisterViewWithRegion(regionName, typeof(TestItemA));
            regionManager.RegisterViewWithRegion(regionName, typeof(TestItemB));
            regionManager.RegisterViewWithRegion(regionName, typeof(TestItemC));
            regionManager.RegisterViewWithRegion(regionName, typeof(TestItemD));
            regionManager.RegisterViewWithRegion(regionName, typeof(TestItemE));
            regionManager.RegisterViewWithRegion(regionName, typeof(TestItemF));
            regionManager.RegisterViewWithRegion(regionName, typeof(TestItemG));
            regionManager.RegisterViewWithRegion(regionName, typeof(TestItemH));
            regionManager.RegisterViewWithRegion(regionName, typeof(SubmitView));
            InitializeComponent();
            this.Closing += TeacherWindow_Closing;
            this.Loaded += TeacherWindow_Loaded;

        }

        private void TeacherWindow_Loaded(object sender, RoutedEventArgs e)
        {
            aggregator.GetEvent<ChangeTestEvent>().Subscribe(NextTest);
            aggregator.GetEvent<ShowMessageEvent>().Subscribe(OnShowMessage);
            aggregator.GetEvent<StartTestEvent>().Subscribe(OnStartTest);
            MemberInfo member = GlobalModel.Instance.MemberInfo;
            aggregator.GetEvent<ChangeTestEvent>().Publish(member.testItem);
        }

        private void OnStartTest()
        {
            int p = GlobalModel.Instance.MemberInfo.testItem;
            aggregator.GetEvent<ChangeTestEvent>().Publish(p);
        }
        private void OnShowMessage(EventInfo info)
        {
            if (info.Type != "test") return;
            string msg = (string)info.Data["msg"];
            string title = (string)info.Data["title"];
            var ele = Keyboard.FocusedElement;
            Keyboard.Focus(null);
            MessageBox.Show(this, msg, title, MessageBoxButton.OK, MessageBoxImage.Information);
            Keyboard.Focus(ele);
        }
        private void TeacherWindow_Closing(object? sender, System.ComponentModel.CancelEventArgs e)
        {
            e.Cancel = true;
            this.aggregator.GetEvent<EndTestEvent>().Publish();
            this.Hide();
        }

        public void NextTest(int p)
        {
            switch (p)
            {
                case 0:
                    regionManager.RequestNavigate(regionName, nameof(TestItemA));
                    break;
                case 1:
                    regionManager.RequestNavigate(regionName, nameof(TestItemB));
                    break;
                case 2:
                    regionManager.RequestNavigate(regionName, nameof(TestItemC));
                    break;
                case 3:
                    regionManager.RequestNavigate(regionName, nameof(TestItemD));
                    break;
                case 4:
                    regionManager.RequestNavigate(regionName, nameof(TestItemE));
                    break;
                case 5:
                    regionManager.RequestNavigate(regionName, nameof(TestItemF));
                    break;
                case 6:
                    regionManager.RequestNavigate(regionName, nameof(TestItemG));
                    break;
                case 7:
                    regionManager.RequestNavigate(regionName, nameof(TestItemH));
                    break;
                case 8:
                    regionManager.RequestNavigate(regionName, nameof(SubmitView));
                    break;
            }
            this.aggregator.GetEvent<TestLoadEvent>().Publish(p);
        }

        private void BtnClose_OnClick(object sender, RoutedEventArgs e)
        {
            SoundPlayer.Stop();
            this.Close();
        }
    }
}
